package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemFeignClient;
import com.hmall.common.client.UserFeignClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.ItemDto;
import com.hmall.common.thread.User;
import com.hmall.common.thread.UserThreadLocal;
import com.hmall.order.dto.OrderDto;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 订单业务
 */
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {


    @Autowired
    private ItemFeignClient itemFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;
    /**
     * 创建订单
     *
     * @param dto
     * @return
     */
    @Override
    public Long createOrder(OrderDto dto) {
        //1.新增order表
        Order order=new Order();
            //1.1 封装数据
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
            //1.2 获取总金额，单价*数量
        //根据商品id远程调用tb_item表获取商品单价
        ItemDto itemDto = itemFeignClient.findById(dto.getItemId());

        order.setTotalFee(itemDto.getPrice()*dto.getNum());
        order.setStatus(1);
            //从threadLocal中获取用户id
        User user = UserThreadLocal.getUser();
        order.setUserId(user.getId());
        order.setPaymentType(dto.getPaymentType());
        save(order);

        //2.新增order-detail表
        OrderDetail orderDetail=new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setImage(itemDto.getImage());
        orderDetail.setSpec(itemDto.getSpec());
        orderDetail.setPrice(itemDto.getPrice());
        orderDetail.setNum(dto.getNum());
        orderDetail.setName(itemDto.getName());
        orderDetail.setCreateTime(new Date());
        orderDetail.setItemId(dto.getItemId());
        orderDetail.setUpdateTime(new Date());

        orderDetailMapper.insert(orderDetail);

        //3.新增tb_order_logistics表

        OrderLogistics orderLogistics=new OrderLogistics();
        orderLogistics.setOrderId(order.getId());
        orderLogistics.setCreateTime(new Date());

        //3.1 根据地址id远程调用tb_address表获取地址信息
        Address address = userFeignClient.findAddressById(dto.getAddressId());

        orderLogistics.setMobile(address.getMobile());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setStreet(address.getStreet());
        orderLogistics.setContact(address.getContact());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setUpdateTime(new Date());
        orderLogisticsMapper.insert(orderLogistics);

        //4.扣减商品的库存
        itemFeignClient.koujianStock(dto.getItemId(),dto.getNum());


        //返回订单id
        return order.getId();
    }
}
